package com.chuangke.sinotrukbudgetapi.util;

import java.util.Date;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSONObject;
import com.chuangke.common.utils.DateUtils;
import com.chuangke.sinotrukbudgetapi.config.ZgzqPoConfig;
import com.chuangke.sinotrukbudgetapi.config.ZgzqPoConfigUtil;

public class POUtil {

    public static String send(String interfaceId, JSONObject request) {
        ZgzqPoConfig config = ZgzqPoConfigUtil.getConfig(interfaceId);

        RestTemplate restTemplate = new RestTemplateBuilder()
                .basicAuthentication(config.getUsername(), config.getPassword()).build();
        // 发送请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(config.getUrl(), request, String.class);
        return responseEntity.getBody();
    }

    public static JSONObject createRequest(String interfaceId, String uuid, JSONObject requestBody) {
        JSONObject response = new JSONObject();
        response.put("MessageHeader", craateMessageHeader("BPCI003", uuid));
        response.put("Requests", requestBody);
        return response;
    }

    public static JSONObject createResponse(String interfaceId, String uuid, JSONObject responseBody) {
        JSONObject response = new JSONObject();
        response.put("MessageHeader", craateMessageHeader("BPCI002_B", uuid));
        response.put("Responses", responseBody);
        return response;
    }

    private static JSONObject craateMessageHeader(String interfaceId, String uuid) {
        JSONObject messageHeader = new JSONObject();
        Date date = new Date();
        messageHeader.put("Interface_ID", interfaceId);
        messageHeader.put("UUID", uuid);
        messageHeader.put("MessageId", "");
        messageHeader.put("Sender", "BPAP");
        messageHeader.put("Receiver", "BPC");
        messageHeader.put("SendDate", DateUtils.getDate(date, "yyyyMMdd"));
        messageHeader.put("SendTime", DateUtils.getDate(date, "HHmmss"));

        return messageHeader;

    }
}
